﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.FileProviders;

namespace LS.Screening.Core
{
    /// <summary>
    /// Application扩展类
    /// </summary>
    public static class ApplicationExtensions
    {
        /// <summary>
        /// 自定义扩展方法
        /// </summary>
        /// <param name="application"></param>
        /// <param name="checkDirectory"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCustomStaticFiles(this IApplicationBuilder application, bool checkDirectory = false)
        {
            if (checkDirectory)
            {
                if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName)))
                    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName));
            }
            application.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName)),
                RequestPath = StringConstants.DefaultRequestPath,
                OnPrepareResponse = (c) =>
                {
                    c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                    c.Context.Response.Headers.Add("cache-control", new[] { "public,max-age=31536000" });
                    c.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
                },
            });
            return application;
        }

        /// <summary>
        /// 自定义扩展方法
        /// </summary>
        /// <param name="application"></param>
        /// <param name="requestPath"></param>
        /// <param name="directoryName"></param>
        /// <param name="checkDirectory"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCustomStaticFiles(this IApplicationBuilder application, string requestPath, string directoryName, bool checkDirectory = false)
        {
            if (checkDirectory)
            {
                if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName)))
                    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName));
            }
            application.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName)),
                RequestPath = requestPath,
                OnPrepareResponse = (c) =>
                {
                    c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                    c.Context.Response.Headers.Add("cache-control", new[] { "public,max-age=31536000" });
                    c.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
                },
            });
            return application;
        }

        /// <summary>
        /// 自定义扩展方法
        /// </summary>
        /// <param name="application"></param>
        /// <param name="checkDirectory"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCustomDirectoryBrowser(this IApplicationBuilder application, bool checkDirectory = false)
        {
            if (checkDirectory)
            {
                if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName)))
                    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName));
            }
            application.UseDirectoryBrowser(new DirectoryBrowserOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StringConstants.DefaultDirectoryName)),
                RequestPath = StringConstants.DefaultRequestPath
            });
            return application;
        }

        /// <summary>
        /// 自定义扩展方法
        /// </summary>
        /// <param name="application"></param>
        /// <param name="requestPath"></param>
        /// <param name="directoryName"></param>
        /// <param name="checkDirectory"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCustomDirectoryBrowser(this IApplicationBuilder application, string requestPath, string directoryName, bool checkDirectory = false)
        {
            if (checkDirectory)
            {
                if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName)))
                    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName));
            }
            application.UseDirectoryBrowser(new DirectoryBrowserOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryName)),
                RequestPath = requestPath
            });
            return application;
        }
    }
}
